<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cat="http://tecnologie-web.studenti.math.unipd.it/tecweb/~ccardin/"
    exclude-result-prefixes="cat">
        
<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes' omit-xml-declaration='yes'/>
    
    <xsl:variable name="cat" select="'__CAT__'"/>
    <xsl:variable name="subcat" select="'__SUBCAT__'"/>
    
    <xsl:template match="/">
        <div id="searchbar">
			<form method="post" action="forumsearch.cgi">
				<div>
					<label for="query">Search:</label>
					<input type="text" id="query" name="query"/>
					<input type="submit" value="Search"/>
				</div>
			</form>
		</div>
        <div id="content-title">
	<h2>
		<span>FORUM</span>
                Share information with the community
	</h2>
        </div>
        __NEW_ALG_LINK__
        <xsl:apply-templates select="cat:algorithms/cat:category[position()=$cat]/cat:subCategory[position()=$subcat]"/>
    </xsl:template>
    
    <xsl:template match="cat:subCategory">
        <div id="content-algorithms">
            <h3>Algorithms for <xsl:value-of select="../cat:nameCategory" /> - <xsl:value-of select="cat:nameSubCategory" /></h3>
            <!--<h4>Subcategory description: <xsl:value-of select="cat:descSubCategory"/></h4>-->
            
            <xsl:choose>
                <xsl:when test="cat:algorithm">
                        <xsl:for-each select="cat:algorithm">
                                <dl class="content-item">
                                        <dd class="item-description"><a href="showAlgorithm.cgi?idAlgorithm={@id}&amp;category={$cat}&amp;subcategory={$subcat}"><xsl:value-of select="cat:description" /></a></dd>
                                        <dd class="item-pos1">Total Comments: <xsl:value-of select="count(.//cat:comment)"/></dd>
                                        <xsl:variable name="numVotes" select="count(./cat:ratings//cat:vote)" />
                                        <xsl:choose>
                                                <xsl:when test="$numVotes=0">
                                                        <dd class="item-pos2">Rating: no rating</dd>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                        <xsl:variable name="rate" select="round(sum(./cat:ratings//cat:vote/cat:value) div $numVotes)" />
                                                   <dd class="item-pos2">
                                                                Rating: <img src="../public_html/images/star{$rate}.gif" alt="rate {$rate} of 5"/>
                                                   </dd>
                                                </xsl:otherwise>
                                        </xsl:choose>
                                </dl>
                        </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
					There are no posts in this section yet.
                </xsl:otherwise>
            </xsl:choose>
            
        </div>
    </xsl:template>
    
</xsl:stylesheet>
